from PyQt4 import QtGui,QtCore,Qt
import sys
from VerbsListWidget import VerbsListWidget
from VerbManager import VerbManager
from Util import FilterableList,FilterableListGraphicsItem,testverb


class MainWidget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        desktop=app.desktop()
        #self.setWindowFlags(self.windowFlags()|Qt.Qt.FramelessWindowHint|Qt.Qt.X11BypassWindowManagerHint)
        self.VERBS=VerbManager().VERBS
        print self.VERBS
        self.scene = QtGui.QGraphicsScene()
        self.VerbIsActive=False
        self.scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
        geom=QtCore.QRectF(desktop.screenGeometry())
        geom.setWidth(geom.width()-4)
        geom.setHeight(geom.height()-4)
        self.scene.setSceneRect(geom)
        self.verblist=FilterableList.FilterableList(200,300)
        self.verblist.move(10,100)
        self.verblist.setWindowOpacity(0.75)
        self.verblist.setvalues(self.VERBS.keys())
        self.verblistproxy=self.scene.addWidget(self.verblist)
        self.view = QtGui.QGraphicsView(self.scene)
        self.view.setRenderHint(QtGui.QPainter.Antialiasing)
        self.view.setBackgroundBrush(QtGui.QBrush(QtGui.QPixmap('/home/darsh/Data/wallpapers/FILE0012.CHK')))
        self.view.setWindowTitle("VerbDesk")
        self.MainLayout=QtGui.QHBoxLayout()
        self.MainLayout.setMargin(0)
        self.MainLayout.addWidget(self.view)
        self.setLayout(self.MainLayout)
        #self.setWindowOpacity(0.81)
        QtCore.QObject.connect(self.verblist,QtCore.SIGNAL("valueselectiondone()"),self.fireverb)
        QtCore.QMetaObject.connectSlotsByName(self)
        

    def advancestep(self):
        if self.stepno<=self.currentverbtotalsteps:
            self.widget=self.currentverb.Execute(self.stepno,self.result)
            if self.widget is not None:
                # self.MainLayout.addWidget(self.widget)
                self.proxywidget=self.scene.addWidget(self.widget)
                self.widget.move(QtCore.QPoint(400,100))
                self.widget.setWindowOpacity(0.67)
                self.scene.update()
                QtCore.QObject.connect(self.widget,QtCore.SIGNAL("valueselectiondone()"),self.completestep)
                QtCore.QObject.connect(self.widget,QtCore.SIGNAL("valueselectioncancelled()"),self.endverb)
                QtCore.QMetaObject.connectSlotsByName(self)
            else:
                print "Found no widget"
                self.endverb()
                
        else:
            print "Ended"
            self.endverb()

    def completestep(self):
        "Print completing"
        self.result=self.widget.getvalue()
        if not self.result:
            self.endverb()
        self.widget.close()
        del self.widget
        self.scene.update(0,0,self.scene.width(),self.scene.height())
        self.stepno=self.stepno+1
        self.advancestep()

    def endverb(self):
        print "Ending verb"
        if self.widget:
            #self.widget.hide()
            self.widget.close()
            del self.widget
        self.VerbIsActive=False

    def fireverb(self):
        if not self.VerbIsActive:
            self.VerbIsActive=True
            #Instantiate the verb
            print self.VERBS
            tv=self.VERBS[str(self.verblist.getvalue())]
            self.stepno=1
            self.result=None
            self.currentverb=tv
            self.currentverbtotalsteps=tv.VERBSTEPS
            #Take the first step
            self.advancestep()
        else:
            print "VERB IS ACTIVE"
        
        
    
if __name__=="__main__":
    app=QtGui.QApplication(sys.argv)    
    #opeffect=QtGui.QGraphicsBlurEffect()
    Form = MainWidget()
    #Form.resize(1040,840)
    Form.setWindowTitle("VerbDesk")
    #Form.setWindowOpacity(0.86)
    #Form.setFixedSize(640,480)
    # Form.setWindowOpacity(0.7)
    #ui = VerbsListWidget()
    #ui.setupUi(Form)
    #Form.setEffect(opeffect)
    #view.setDragMode(QtGui.QGraphicsView.ScrollHandDrag)
    #view.setAttribute(Qt.Qt.WA_TranslucentBackground, True)
    #view.show()
    Form.showFullScreen()
    #Form.fireverb()
    #  Form.fireverb()
    sys.exit(app.exec_())